//
//  ResetPasswordVC.m
//  CenturyCity
//
//  Created by beisheng03 on 2018/1/15.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "ResetPasswordVC.h"
#import "RegisterCommonCell.h"
#import "WritePersonInfoVC.h"
#import "RegSecurityCodeCell.h"
#import "RegisterPwdCell.h"
#import "RegPwdSureCell.h"
#import "MyInfoFooterView.h"
#import "RegisterEmailCell.h"


@interface ResetPasswordVC ()

@property (nonatomic, strong) MyInfoFooterView *footerView;


@end

@implementation ResetPasswordVC
- (RegisterInfoModel *)registerModel{
    
    if (!_registerModel) {
        
        _registerModel = [[RegisterInfoModel alloc]init];
        
        _registerModel.type = @"reset";
        
    }
    
    
    return  _registerModel;
}


- (MyInfoFooterView *)footerView{
    
    if (!_footerView) {
        
        MyInfoFooterView *temp = (MyInfoFooterView *)[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([MyInfoFooterView class]) owner:nil options:nil].firstObject;
        
        temp.bottomLabel.hidden = YES;
        
        temp.topBtn.backgroundColor = themeColor;
        
        [temp.topBtn setTitle:@"重置密码" forState:UIControlStateNormal];
        
        [temp.topBtn addTarget:self action:@selector(nextStepDidClick) forControlEvents:UIControlEventTouchUpInside];
        
        _footerView = temp;
        
    }
    
    return _footerView;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    
}

- (void)viewWillDisappear:(BOOL)animated{
    
    
    [super viewWillDisappear:animated];
    
    if (self.timer) {
        [self.timer invalidate];
    }
    
    [self.view endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"忘记密码";
    
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    //    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下一步" style:UIBarButtonItemStylePlain target:self action:@selector(nextStepDidClick)];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([RegisterCommonCell class]) bundle:nil] forCellReuseIdentifier:[RegisterCommonCell identifierStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([RegSecurityCodeCell class]) bundle:nil] forCellReuseIdentifier:[RegSecurityCodeCell identifierStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([RegisterPwdCell class]) bundle:nil] forCellReuseIdentifier:[RegisterPwdCell identifierStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([RegPwdSureCell class]) bundle:nil] forCellReuseIdentifier:[RegPwdSureCell identifierStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([RegisterEmailCell class]) bundle:nil] forCellReuseIdentifier:[RegisterEmailCell identifierStr]];
    
    self.tableView.tableFooterView = self.footerView;
    
    //    self.tableView.tableFooterView = [[UIView alloc]init];
    // Do any additional setup after loading the view.
}



/*
 发送邮件接口成功后会返回发送的验证码，和用户输入的验证码进行比对
 email    用户输入的邮箱
 http://feidu.baonongkang.cn/index.php/api/Member/sendEmail
 */
- (void)nextStepDidClick{
    
    [self.view endEditing:YES];
    
    
    if (self.registerModel.email == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请填写手机号"];
        
        return;
    }
    
    
    BOOL isEmail = [[NetworkTools shareNetworkTools] isEmailAdress:self.registerModel.email];
    
    if (!isEmail) {
        
        [SVProgressHUD showInfoWithStatus:@"手机号格式不正确!"];
        
        return;
        
    }
    
    
    //    if (self.registerModel.userCaptcha == nil) {
    //
    //        [SVProgressHUD showInfoWithStatus:@"请填写手机号"];
    //
    //        return;
    //    }
    //
    //
    //    BOOL istelephoneNumber = [[NetworkTools shareNetworkTools] isPhoneNumber:self.registerModel.phoneNumber];
    //
    //    if (!istelephoneNumber) {
    //
    //        [SVProgressHUD showInfoWithStatus:@"手机号格式不正确!"];
    //
    //        return;
    //
    //    }
    
    
    if (self.registerModel.userCaptcha == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请填写验证码"];
        
        return;
    }
    
    BOOL isCaptcha = [[NetworkTools shareNetworkTools] isCaptcha:self.registerModel.userCaptcha];
    
    if (!isCaptcha) {
        
        [SVProgressHUD showInfoWithStatus:@"验证码格式不正确!"];
        
        return;
        
    }
    
    

    
    
    if (self.registerModel.password == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请填写新密码"];
        
        return;
    }
    
    
    
    if (self.registerModel.password.length <  6) {
        
        [SVProgressHUD showInfoWithStatus:@"密码长度至少六位"];
        
        return;
    }
    
    
    if (![self.registerModel.password isEqualToString:self.registerModel.surePassword]) {
        
        [SVProgressHUD showInfoWithStatus:@"两次输入密码不一致"];
        
        return;
    }
    
    
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"mobile"] = self.registerModel.email;
    //    parameters[@"phone"] = self.registerModel.phoneNumber;
    parameters[@"password"] = self.registerModel.password;
    parameters[@"Repassword"] = self.registerModel.password;
    parameters[@"code"] = self.registerModel.userCaptcha;
    parameters[@"type"] = @"1";

    MJWeakSelf
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"Member/register" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            [SVProgressHUD showSuccessWithStatus:@"修改成功!"];
            
            [weakSelf.navigationController performSelector:@selector(popViewControllerAnimated:) withObject:@1 afterDelay:2.0];
            
            
        } else {
            
            [SVProgressHUD showErrorWithStatus:error.domain];
            
        }
    }];
    
    
}


//

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 5;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        RegisterEmailCell *cell = [tableView dequeueReusableCellWithIdentifier:[RegisterEmailCell identifierStr]];
        
        cell.model = self.registerModel;
        
        return cell;
        
    } else if (indexPath.row == 1){
        
        RegisterCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:[RegisterCommonCell identifierStr]];
        
        cell.model = self.registerModel;
        
        return cell;
        
    }else if (indexPath.row == 2){
        
        RegisterPwdCell *cell = [tableView dequeueReusableCellWithIdentifier:[RegisterPwdCell identifierStr]];
        cell.model = self.registerModel;
        
        return cell;
        
    }else if (indexPath.row == 3){
        
        RegPwdSureCell *cell = [tableView dequeueReusableCellWithIdentifier:[RegPwdSureCell identifierStr]];
        
        cell.model = self.registerModel;
        
        return cell;
        
    }else{
        
        return [[UITableViewCell alloc]init];
    }
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 4) {
        return 0;
    } else {
        
        if (indexPath.row == 3) {
            return 52;
        } else {
            return 72;
        }
        
    }
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    [self.view endEditing:YES];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}


@end
